window.onload = function () {
    initRootTop()
    initRootFooter()

    initMainBar()
    initHotPost()
    initAdvertisement()
    editor = new Simditor({
        textarea: $('#main-question-editor')
        //optional options
    });

    try_editor = new Simditor({
        textarea: $('#try-editor')
        //optional options
    });
    initLogin()
    toLogin(true)


}


function del_tag(obj) {
    console.log(obj);
    obj.remove()
}

function add_tag() {
    let tag_input = document.querySelector("#tag-input")
    let tag_list = document.querySelector("#tag-list")
    if (tag_input.value !== '') {
        tag_list.innerHTML += `<li class="tag-item" onclick="del_tag(this)">${tag_input.value.toUpperCase()}</li>`
        tag_input.value = ""
    } else {
        alert("标签输入框不能为空")
    }
}


function sendPost() {
    // console.log(editor.getValue());
    // console.log(try_editor.getValue());
    let post_title = document.querySelector("#post-title").value
    let tag_list = document.querySelector("#tag-list").children
    let tagString = ""
    let tags = []
    for (var i = 0; i < tag_list.length; i++) {
        tags.push({'tagTitle': tag_list[i].innerHTML})
    }

    if (checkLogin() === false) {
        scrollTo(0, 0)
        toLogin()
        return
    }


    let post = {
        'postTitle': post_title,
        'whatPostContent': {
            postContent: editor.getValue() + '<br/>' + try_editor.getValue()
        },
        'whatTags': tags
    }
    console.log(post)

    ajax("post", "/post/sendpost", JSON.stringify(post), function (json) {

        if (json.code === 401) {

            toLogin()
        } else if (json.code === 200) {
            console.log('发帖成功' + json.data)
            alert("发帖成功")
            window.location.href = webUrl + '/index.html'
        } else {
            alert("未知错误")
        }
    }, function (json) {
    }, false)

}